package org.ruanjj.stringreverse;

public class Solution1 {
    public String reverseMessage(String message){
        message = message.trim();
        StringBuilder res = new StringBuilder();
        int j = message.length()-1;
        int i = j;
        while(i > -1){
            while(i>-1 && message.charAt(i) != ' ') i--;
            res.append(message.substring(i+1,j+1)+" ");
            while(i>-1 && message.charAt(i) == ' ') i--;
            j = i;
        }
        return res.toString().trim();
    }
}
